package net.sureon.web.user;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sureon.app.UserSession;
import net.sureon.biz.ApplicationMgr;
import net.sureon.common.SureonConstants;
import net.sureon.dao.Application;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

/**
 * 处理用户申请 获得用户所有的申请
 * 
 * @author Administrator
 * 
 */
public class MyApplication implements Controller {
	
	private ApplicationMgr applicationMgr;

	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		int maxResults = SureonConstants.PAGE_SIZE;
		String cp = request.getParameter("currentPage");
		int currentPage = 1;
		if(!"".equals(cp)) {
			try {
				currentPage = Integer.parseInt(request.getParameter("currentPage"));
			} catch(Exception e) {
				
			}
		}
		
		int skipResults = (currentPage - 1) * maxResults;
		
		
		
		List<Application> all = null;
		long userID = ((net.sureon.app.UserSession) request.getSession()
				.getAttribute(UserSession.HTTP_SESSION_KEY)).getUserID();

		long companyID = ((net.sureon.app.UserSession) request.getSession()
				.getAttribute(UserSession.HTTP_SESSION_KEY)).getCompanyID();

		String showContent = request.getParameter("showContent");

		if (showContent != null && "yes".equals(showContent)) {
			long applicationID = Long.parseLong(request
					.getParameter("applicationID"));
			Application app = applicationMgr.getApplicationByID(companyID,
					applicationID);
			request.setAttribute("app", app);
		}
		all = applicationMgr.getApplicationByUserNotReview(companyID, userID, skipResults, maxResults);
		request.setAttribute("all", all);
		int pageCounter = (applicationMgr.getApplicationByUserNotReview(companyID, userID, 0, 10000).size() + SureonConstants.PAGE_SIZE - 1) / SureonConstants.PAGE_SIZE;
		request.setAttribute("currentPage", currentPage);
		request.setAttribute("pageCounter", pageCounter);

		return new ModelAndView("user/myApplication");
	}

	public ApplicationMgr getApplicationMgr() {
		return applicationMgr;
	}

	public void setApplicationMgr(ApplicationMgr applicationMgr) {
		this.applicationMgr = applicationMgr;
	}
}
